<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
<!--      <el-form-item label="地点" prop="place">-->
<!--        <el-input-->
<!--          v-model="queryParams.place"-->
<!--          placeholder="请输入地点"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:activity:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-bell"
          size="mini"
        >提醒</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:activity:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:activity:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="活动ID" align="center" prop="activityId" />-->
      <el-table-column type="index" width="50" />
      <el-table-column label="标题" align="center" prop="title" min-width="180" />
      <el-table-column label="活动时间" align="center" prop="startTime" width="180">
      </el-table-column>
      <el-table-column label="地点" align="center" prop="place" width="100" />
      <el-table-column label="创建者" align="center" prop="creater" width="80" />
      <el-table-column label="创建时间" align="center" prop="syncTime" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['community:activity:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['community:activity:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >统计</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleRegister(scope.row)"
          >签到</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" maxlength="45" placeholder="请输入标题" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="内容" prop="content">
            <el-input
              v-model="form.content"
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 15}"
              placeholder="请输入内容"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                v-model="form.startTime"
                clearable
                size="small"
                type="datetime"
                placeholder="选择开始时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                v-model="form.endTime"
                clearable
                size="small"
                type="datetime"
                placeholder="选择结束时间"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地点" prop="place">
              <el-input v-model="form.place" size="small" placeholder="请输入地点" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 显示活动报名和签到情况的对话框 -->
    <el-dialog title="活动统计情况" :visible.sync="statsOpen" width="800px" append-to-body>
      <div class="context-title">
        {{ activityTitle }}
      </div>
      <span style="font-size: large; font-weight: bolder; ">报名人数：{{ enrollNumber }}人</span>
      <el-table :data="enrollTabelData" style="width: 100%; margin-top: 10px; margin-bottom: 10px" max-height="250">
        <el-table-column prop="time" label="报名时间" width="150" />
        <el-table-column prop="userName" label="用户名" :show-overflow-tooltip="true" width="120"/>
        <el-table-column prop="nickName" label="用户昵称"/>
        <el-table-column prop="sex" label="性别" width="80"/>
        <el-table-column prop="phoneNumber" label="电话号码"/>
      </el-table>
      <span style="font-size: large; font-weight: bolder; ">参与人数：{{ participateNumber }}人</span>
      <el-table :data="participateTabelData" style="width: 100%; margin-top: 10px; margin-bottom: 10px" max-height="250">
        <el-table-column prop="time" label="签到时间" width="150" />
        <el-table-column prop="userName" label="用户名" :show-overflow-tooltip="true" width="120"/>
        <el-table-column prop="nickName" label="用户昵称"/>
        <el-table-column prop="sex" label="性别" width="80"/>
        <el-table-column prop="phoneNumber" label="电话号码"/>
      </el-table>
      <el-button @click="statsOpen = false">取 消</el-button>
    </el-dialog>

    <!-- 显示活动签到码对话框 -->
    <el-dialog title="活动签到验证码" :visible.sync="codeOpen" append-to-body>
      <div class="context-title">
        "{{ activityTitle }}"签到验证码
      </div>
      <div style="text-align: center">
        <span style="font-size: 80px; font-weight: bolder">{{ registerCode }}</span>
      </div>
      <div style="text-align: center;">
        <el-button @click="codeOpen = false" style="margin: 20px auto">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from '@/api/cpc/activity'
import { listEnrollById, getEnroll, delEnroll, listParticipateById } from '@/api/cpc/activityDetail'
import { dateFormat } from '@/utils/dateUtils'

export default {
  name: 'Activity',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动列表表格数据
      activityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否显示活动报名与参与统计页面弹出层
      statsOpen: false,
      // 是否显示签到验证码页面弹出层
      codeOpen: false,
      // 活动名
      activityTitle: null,
      // 活动开始时间搜索范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        startTime: null,
        endTime: null,
        place: null,
        creater: null,
        syncTime: null,
        viewNumber: null,
        enrollNumber: null,
        participateNumber: null,
        communityId: null
      },
      // 表单参数
      form: {},
      // 查看活动详情对话框内的相关变量
      viewActivityFlag: false,
      enrollNumber: 0,
      participateNumber: 0,
      enrollTabelData: [],
      participateTabelData: [],
      registerCode: null,
      // 表单校验
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        place: [
          { required: true, message: '地点不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询活动列表列表 */
    getList() {
      this.loading = true
      listActivity(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.activityList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        title: null,
        content: null,
        startTime: null,
        endTime: null,
        place: null,
        creater: null,
        syncTime: null,
        viewNumber: null,
        enrollNumber: null,
        participateNumber: null,
        communityId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加活动列表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const activityId = row.activityId || this.ids
      getActivity(activityId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改活动列表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.activityId != null) {
            updateActivity(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            const createTime = new Date()
            this.form.syncTime = dateFormat('YYYY-mm-dd HH:MM:SS', createTime)
            this.form.startTime = dateFormat('YYYY-mm-dd HH:MM:SS', this.form.startTime)
            if (this.form.endTime) {
              this.form.endTime = dateFormat('YYYY-mm-dd HH:MM:SS', this.form.endTime)
            }
            this.form.viewNumber = 0
            this.form.enrollNumber = 0
            this.form.participateNumber = 0
            addActivity(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const activityIds = row.activityId || this.ids
      this.$confirm('是否确认删除活动列表编号为"' + activityIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delActivity(activityIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 统计按钮操作 */
    handleView(row) {
      // this.$router.push('/cpc/activityInfo/' + row.activityId)
      this.activityTitle = row.title
      this.enrollNumber = row.enrollNumber
      this.participateNumber = row.participateNumber
      listEnrollById(row.activityId).then(response => {
        this.enrollTabelData = []
        const data = response.data
        for (let i = 0; i < data.length; i++) {
          const enroll = data[i].record
          const user = data[i].userInfo
          // console.log('报名记录', enroll)
          // console.log('用户信息', user)
          this.enrollTabelData.push({
            'id': enroll.id,
            'acticityId': enroll.acticityId,
            'userId': enroll.userId,
            'time': enroll.time,
            'userName': user.userName,
            'nickName': user.nickName,
            'phoneNumber': user.phonenumber,
            'sex': user.sex === 0 ? '男' : '女'
          })
        }
      })
      listParticipateById(row.activityId).then(response => {
        this.participateTabelData = []
        const data = response.data
        for (let i = 0; i < data.length; i++) {
          const participate = data[i].record
          const user = data[i].userInfo
          this.participateTabelData.push({
            'id': participate.id,
            'acticityId': participate.acticityId,
            'userId': participate.userId,
            'time': participate.time,
            'userName': user.userName,
            'nickName': user.nickName,
            'phoneNumber': user.phonenumber,
            'sex': user.sex === 0 ? '男' : '女'
          })
        }
      })
      this.statsOpen = true
    },

    handleRegister(row) {
      this.activityTitle = row.title
      this.registerCode = row.registerCode
      // console.log('签到码', row)
      this.codeOpen = true
    }

  }
}
</script>

<style>
.noticeDialog {
  max-height: 370px;
  overflow-y: scroll;
}
.noticeDialog::-webkit-scrollbar {
  display: none;
}
pre {
  white-space: pre-wrap;       /* css-3 */
  word-wrap: break-word;       /* Internet Explorer 5.5+ */
  word-break: break-all;
  overflow: hidden;
  color: #444444;
  font-size: 16px;
  line-height: 32px;
}
.context-title {
  font-size: 24px;
  color: #444444;
  text-align: center;
  font-weight: bold;
  line-height: 38px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ddd;
  margin-bottom: 20px;
}
.time {
  color: #999;
  font-size: 14px;
  line-height: 20px;
  height: 20px;
  margin-left: 15px;
}
</style>
